package com.gitee.hljdrl.dsbridge.jsapi;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class DSPluginProvider {

    private static Map<String, Class<? extends DSPlugin>> mRegeterMap = new HashMap<>();

    private static DSPluginProvider INSTANCE = null;

    public static DSPluginProvider getInstance() {
        if (INSTANCE == null) {
            synchronized (DSPluginProvider.class) {
                INSTANCE = new DSPluginProvider();
            }
        }
        return INSTANCE;
    }

    protected DSPluginProvider() {
    }

    public void register(String name, Class<? extends DSPlugin> plugin) {
        if (!mRegeterMap.containsKey(name)) {
            mRegeterMap.put(name, plugin);
        }
    }

    public void unRegister(String name) {
        if (mRegeterMap.containsKey(name)) {
            mRegeterMap.remove(name);
        }
    }

    public Map<String, Class<? extends DSPlugin>> getRegeterMap() {
        Map<String, Class<? extends DSPlugin>> copy = new HashMap<>();
        if (!mRegeterMap.isEmpty()) {
            copy.putAll(mRegeterMap);
        }
        return copy;
    }

    public Collection<Class<? extends DSPlugin>> getList() {
        List<Class<? extends DSPlugin>> list = new ArrayList<>();
        if (!mRegeterMap.isEmpty()) {
            list.addAll(mRegeterMap.values());
        }
        return list;
    }
}
